package com.huiyeji.dynamic.core;

public enum DynamicRegex {

    PREFIX("动态语句前缀", "[a-zA-Z]+"),
    RESULT_NAME("函数返回对象名", "[a-zA-Z]+[\\.@#$&]{0,1}[a-zA-Z\\d]*"),
    EQUALS("等于号", "="),
    DECIMAL_POINT("小数点", "\\."),
    OBJECT_NAME("动态对象名", "[a-zA-Z]+[\\.@#$&]{0,1}[a-zA-Z\\d]*"),

    METHOD("动态对象方法名", "[a-zA-Z0-9]+\\((?:[^()]*|\\([^()]*\\))*\\)"),
    PARAM_NULL("参数-null", "(null|NULL)"),
    PARAM_INTEGER("参数-integer", "-?\\d+"),
    PARAM_CONTEXT_OBJECT_NAME("参数-上下文对象", "[a-zA-Z]+[\\.@#$&]{0,1}[a-zA-Z\\d]*"),
    PARAM_STRING("参数-string", "'[^']*'"),
    PARAM_BOOLEAN("参数-boolean", "(true|TRUE|false|FALSE)");

    private final String desc;

    private final String regex;

    DynamicRegex(String desc, String regex){
        this.desc = desc;

        this.regex = regex;
    }

    public String getDesc() {
        return desc;
    }

    public static String build(boolean exactly,String... regexComponents){
        StringBuilder sb = new StringBuilder(exactly? "^\\s*":".*");
        for (String regex : regexComponents) {
            sb.append(regex).append("\\s*");
        }
        return sb.append(exactly? "\\s*$":".*").toString();
    }

    public String origin(){
        return regex;
    }

    public String simple() {
        return simple(true);
    }

    public String simple(boolean require) {
        String realRegex = "\\s*" + regex + "\\s*";
        return require ? realRegex : "(" + realRegex + ")?";
    }

    public String complete() {
        return "^" + simple() + "$";
    }
}
